<?php   
    class UserDAO{
        private $connection;
        
        // Syndesh kai epilogh bashs
        function __construct(){
            $this->connection = mysql_connect('127.0.0.1', 'root', '')
                    or die('Could not connect: ' . mysql_error());
            mysql_select_db('travelbook', $this->connection)
                    or die('Could not select database');
        }

        // Kleisimo syndeshs me thn bash
        function __destruct(){
            if(isset($connection))
                mysql_close($connection);
        }

        // Eisagwgh neou xrhsth sth bash
        function insert($user){
            $query =  "INSERT INTO user VALUES(default, '$user->username', '$user->passwordhash', '$user->email', CURDATE(), '$user->currentLocation')";
            return mysql_query($query);
        }

        // Psaxnei ean yparxei o xrhsths me to sygkekrimeno password
        function search($username, $password){
            $query =  "SELECT * FROM user WHERE username='$username' AND passwordhash='$password'";
            $result = mysql_query($query);
            if(mysql_num_rows($result) == 0){
                return false;
            }
            else{
                return true;
            }
        }

        // Epistrefei to id enos xrhsth
        function retrieveId($username){
            $query =  "SELECT userid FROM user WHERE username='$username'";
            $result = mysql_query($query);
            if(mysql_num_rows($result) == 0){
                return false;
            }
            else{
                $row = mysql_fetch_array($result);
                return $row['userid'];
            }
        }

        // Epistrefei to username tou xrhsth me bash to id
        function retrieveUsername($userid){
            $query = "SELECT username FROM user WHERE userid='$userid'";
            $result = mysql_query($query);
            if(mysql_num_rows($result) == 0){
                return '';
            }
            else{
                $row = mysql_fetch_array($result);
                return $row['username'];
            }
        }

        // Epistrefei to email tou xrhsth me bash to id
        function retrieveEmail($userid){
            $query = "SELECT email FROM user WHERE userid='$userid'";
            $result = mysql_query($query);
            if(mysql_num_rows($result) == 0){
                return false;
            }
            else{
                $row = mysql_fetch_array($result);
                return $row['email'];
            }
        }

        function retrieveCurrentLocation($userid){
            $query = "SELECT currentLocation FROM user WHERE userid='$userid'";
            $result = mysql_query($query);
            if(mysql_num_rows($result) == 0){
                return false;
            }
            else{
                $row = mysql_fetch_array($result);
                return $row['currentLocation'];
            }
        }

        function updateCurrentLocation($userid,$location){
            $query = "UPDATE user SET currentLocation = '$location' WHERE userid='$userid'";
            mysql_query($query);
        }
        
    }
?>
